from common import datasetInfo
from algorithm.classify.c45 import C45
from algorithm.classify.module import Module

class ClassifyGateway:
    def getTableList(self):
        tables = getattr(datasetInfo, 'table_list')
        result = {"tableList":[]}
        i = 1
        for item in tables:
            result["tableList"].append({"num": i, "table": item})
            i = i + 1
        return result

    def getAttributeList(self, table):
        self.mod = Module(table)
        attributes = self.mod.getAttributeInfo()
        return {"attributeList":attributes}

    def discretizeAttribute(self, attribute, granularity):
        result = self.mod.discretizeAttribute(attribute, granularity)
        return result

    def c45Classify(self, class_label_attribute, accuracy, ignore_attribute_list):
        table = self.mod.fetchTable()
        table_name = self.mod.getTableName()
        dm = C45(table_name, table, class_label_attribute, accuracy)
        dm.ignoreAttribute(ignore_attribute_list)
        result = dm.classify()
        return {"result":result}
